<?php

if (!defined('IN_API')) {
	exit('Access Denied');
}
define('API_RETURN_SUCCEED', '1');
define('API_RETURN_FAILED', '-1');
define('API_RETURN_FORBIDDEN', '-2');

error_reporting(E_ALL);

define('API_PATH', dirname(__FILE__));

//defined('MAGIC_QUOTES_GPC') || define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
$cnf = API_PATH . '/config.inc.php';
require_once($cnf);
require_once(API_PATH . '/client.php');
loadcore();
//set_magic_quotes_runtime(0);
$get = $post = array();
$code = $_GET['code'];
parse_str(authcode($code, 'DECODE', HP_KEY), $get);
if (MAGIC_QUOTES_GPC) {
	$get = api_stripslashes($get);
}
$timestamp = time();
if ($timestamp - $get['time'] > 3600) {
	exit('Authracation has expiried');
}
if (empty($get)) {
	exit('Invalid Request');
}
$action = $get['action'];
require libfile('class/xml');
$post = xml2array(file_get_contents('php://input'));

$actlist = get_class_methods('hp_note');
if (in_array($get['action'], $actlist)) {
	/* 加载本地数据库 */
	$hp_note = new hp_note($db);
	$hp_note->get = $get;
	$hp_note->post = $post;
	echo $hp_note->$get['action']();
	exit();
} else {
	exit(API_RETURN_FAILED);
}

class hp_note {

	/**
	 *
	 * @var db_mysql $db
	 */
	var $db = '';

	function hp_note($db) {
		$this->db = & $db;
	}

	/**
	 * 获取子站版本号
	 *
	 * @return string 版本号
	 */
	function getversion() {
		require_once ROOT_PATH . '/source/version.php';
		return JYCMS_VERSION . ' release ' . JYCMS_RELEASE;
	}

	function query() {
		$sql = $this->get['keyvalue'];
		return $this->db->query($sql);
	}

	function upgrade() {
		$fn = $this->get['keyvalue'];
		require_once libfile('class/files');
		require_once libfile('class/pclzip');
		$fn = cmsFS::formatPath($fn, false);
		$savefn = ROOT_PATH . '/data/' . basename($fn);
		$savefn = cmsFS::formatPath($savefn, false);
		if (!file_exists($savefn)) {
			require_once libfile('function/focus');
			$content = dfsockopen(HP_SERVER_URL . '/' . $fn, 'r', true);
			if ($content) {
				cmsFS::write($savefn, $content);
			}
		}
		if (!file_exists($savefn)) {
			return "升级文件下载失败！";
		}
		$unzip = new PclZip($savefn);
		$ret = $unzip->extract(PCLZIP_OPT_PATH, "");
		if ($ret == 0) {
			return("不可恢复的错误： '" . $unzip->errorName(true) . "'解压文件时出错!");
		}
		$filenum = 0;
		$dirnum = 0;
		for ($i = 0; $i < count($ret); $i++) {
			if ($ret[$i]['folder'] == 1) {
				$dirnum+=1;
			} else {
				$filenum+=1;
			}
		}
		@unlink($savefn);
		require_once ROOT_PATH . '/source/version.php';
		$vers = JYCMS_VERSION . '.' . JYCMS_RELEASE;
		return "共解出文件 $filenum 个，文件夹 $dirnum 个。<br>目前的版本是：" . $vers;
	}

	/**
	 * 保存系统配置
	 *
	 * @return boolean
	 */
	function setting() {
		require_once libfile('class/files');
		$cnf = API_PATH . '/config.inc.php';
		@unlink($cnf);
		$result = cmsFS::write($cnf, $this->get['keyvalue']);
		return $result;
	}

	function createsite() {
		require_once libfile('class/files');
		$row = unserialize(decompress($this->get['keyvalue']));
		$config = $row['config'];
		$data = array('skey' => 'sitename', 'svalue' => $row['data']['name']);
		DB::insert('common_setting', $data, 0, 1);
		$data = array('skey' => 'authkey', 'svalue' => $row['data']['authkey']);
		DB::insert('common_setting', $data, 0, 1);
		$data = array('skey' => 'siteurl', 'svalue' => $row['data']['domain']);
		DB::insert('common_setting', $data, 0, 1);
		require_once libfile('function/cache');
		updatecache('setting');
		$result = 1;
		if ($config) {
			$cnf = ROOT_PATH . '/config/config_global.php';
			@unlink($cnf);
			$result = cmsFS::write($cnf, $config);
		}
		return intval($result);
	}

	/**
	 * 获取站点数据库结构
	 *
	 * @return 编码后的数据库结构语句
	 */
	function getdb() {
		global $_G;
		$tablepre = $_G['config']['db']['tablepre'];
		$sql = "SHOW TABLE STATUS LIKE '" . $tablepre . "%'";
		$query = $this->db->query($sql);
		while ($v = $this->db->fetch_array($query)) {
			$sql = "SHOW CREATE TABLE `" . $v['Name'] . "`";
			$queryx = $this->db->query($sql, 'SILENT');
			$row = $this->db->fetch_array($queryx);
			$stru[] = $row;
		}
		return authcode(compress($stru), 'ENCODE', HP_KEY);
	}

	//测试与主站是否联通
	function test() {
		return API_RETURN_SUCCEED;
	}

	function contentrule() {
		$row = unserialize(base64_decode($this->get['keyvalue']));
		$sql = "SELECT id FROM #__focus_contentrule WHERE url='" . $row['url'] . "'";
		$result = $this->db->result_first($sql);
		$row = daddslashes($row);
		unset($row['id']);
		if ($result) {
			$result = DB::update('focus_contentrule', $row, "id='$result'");
		} else {
			$result = DB::insert('focus_contentrule', $row);
		}
		require_once libfile('class/cache');
		$cache = new cache($this);
		$cache->updatedata('contentrule');
		return $result;
	}

	function keywords() {
		$row = unserialize($this->get['keyvalue']);
		$sql = "SELECT id FROM #__focus_keywords WHERE name='" . $row['name'] . "'";
		$data = array('name' => $row['name'], 'local' => $row['local'], 'enabled' => $row['enabled'], 'dateline' => time());
		$result = $this->db->result_first($sql);
		if ($result) {
			$result = DB::update('focus_keywords', $data, "id='$result'");
		} else {
			$result = DB::insert('focus_keywords', $data);
		}
		return $result;
	}

	function getsubsite(){
		$sql = "SELECT * FROM #__common_category WHERE foldername!=''";
		$query = DB::query($sql);
		$rows = array();
		while($row = DB::fetch_array($query)){
			$rows[] = $row;
		}
		return serialize($rows);
	}
	function _serialize($arr, $htmlon = 0) {
		return array2xml($arr, $htmlon);
	}

}
